package com.kingyea.mobilepolice.apimanage.accesscontrol.controller;

import com.kingyea.mobilepolice.apimanage.accesscontrol.bean.AccessController;
import com.kingyea.mobilepolice.apimanage.accesscontrol.handle.Keyword;
import com.kingyea.mobilepolice.apimanage.accesscontrol.service.AccessControllerService;
import com.kingyea.mobilepolice.apimanage.dictionary.service.DictionaryService;
import com.kingyea.mobilepolice.apimanage.log.OperateType;
import com.kingyea.mobilepolice.apimanage.log.OperationLogAnnotation;
import com.kingyea.mobilepolice.apimanage.paraminfo.bean.ParamInfo;
import com.kingyea.mobilepolice.dictionary.bean.Dictionary;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjw
 * @version v1.0.0
 * @description AccessController 控制层
 * @date Created in 2018-04-03 15:31:23
 */
@RequestMapping(value = "/accessController")
@RestController
public class AccessControllerController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AccessControllerController.class);

    @Autowired
    private AccessControllerService accessControllerService;

    @Autowired
    private DictionaryService dicService;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Object list() throws Exception {
        return accessControllerService.list(null);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object detail(@PathVariable Integer id) throws Exception {
        return accessControllerService.getById(id);
    }

    /**
     * 创建
     *
     * @param accessController
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Object create(@RequestBody AccessController accessController) throws Exception {
        return accessControllerService.save(accessController);
    }

    /**
     * 更新
     *
     * @param accessController
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public Object update(@RequestBody AccessController accessController) throws Exception {
        return accessControllerService.update(accessController);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Object delete(@PathVariable Integer id) throws Exception {
        return accessControllerService.deleteById(id);
    }

    @ApiOperation(value = "根据搜索条件搜索接口列表", notes = "", response = AccessController.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "status", value = " 状态：1:注册未审核，2:变更未审核，3:审核通过，4:审核不通过 ", required = false, dataType = "Integer")
            , @ApiImplicitParam(name = "pageNum", value = "第几页", required = true, dataType = "int")
            , @ApiImplicitParam(name = "pageSize", value = "每页数据数量大小", required = true, dataType = "int")
            , @ApiImplicitParam(name = "keyword", value = " 关键字keyword，订单号和代理商名字 ", required = false, dataType = "Integer")
            , @ApiImplicitParam(name = "searchType", value = " 搜索类型（name.接口名称 serialNo.接口标识 organization.管理单位名称  all.所有） ", required = true, dataType = "String")
            , @ApiImplicitParam(name = "policeType", value = " 分类id 列表,对应服务的警种类型 ", required = false, dataType = "Array")
            // 以下参数一般用于适应关联页面列表里边的筛选

    })
    @OperationLogAnnotation(operateName = "根据搜索条件搜索接口列表", operateType = OperateType.SELECT, module = "serviceinfo")
    @RequestMapping(value = "/searchByCondition", method = RequestMethod.POST)
    public Object searchByCondition(@RequestBody Map<String, Object> map) throws Exception {
        return accessControllerService.searchByCondition(map);
    }

    @RequestMapping(value = "/getInfoByServiceId", method = RequestMethod.POST)
    public Object getByServiceId(@RequestBody Map<String, Object> map) throws Exception {
        Integer id = Integer.parseInt(map.get("serviceId").toString());
        AccessController ac = accessControllerService.getByServiceId(id);
        List<Keyword> keywords = accessControllerService.getALLKeyword();
        List<ParamInfo> paramInfos = accessControllerService.getParamInfoByServiceId(id);
        Map<String, Object> dicParam = new HashMap<>();
        dicParam.put("type", "param_rule");
        List<Dictionary> paramRule = dicService.getRootListByType(dicParam);
        Map<String, Object> result = new HashMap<>();
        result.put("acInfo", ac);
        result.put("paramInfo", paramInfos);
        result.put("keyword", keywords);
        result.put("paramRule", paramRule);
        return result;
    }

    @RequestMapping(value = "/addAccInfo", method = RequestMethod.POST)
    public Object addAccinfo(@RequestBody AccessController accessController) throws Exception {
        accessController.getIpBlack();
        accessControllerService.addOrSaveACCInfo(accessController);
        String s = "sss";
        return s;

    }

}
